<?php
/*
 *  supplier 供货商添加类
 *
 *  @param $c 一般是类名
 *  @param $a 一般是方法名
 */

class order extends Admin {

    private $tab = array( 'orderMan' => '添加订单','myOrder' => '我的订单');

    public function __construct() {
        parent::__construct();
        $this->type = G( 'a' ) == 'index' ? '' : G( 'a' );
        $this->pk = "orderNo";
    }

    public function index( $c, $a ) {
        $type = G( 'type', 'orderMan' );
        $method = "{$a}_{$type}";
        $this->$method( $c, $a, $type );
    }

    private function index_orderMan( $c, $a, $type ) {
        $type = G( 'type', 'orderMan' );
        $this->table = 'order';

        $this->page_size = 2;

        $result = $this->Paged( G( 'page' ), $this->page_size )->findAll();

        $pager = $result['page'];
        $result = $result['result'];
        include $this->display( $c );

    }

    private function index_myOrder( $c, $a, $type ) {
        $type = G( 'type', 'myOrder' );
        $this->table = 'order';

        $this->page_size = 2;

        $result = $this->Paged( G( 'page' ), $this->page_size )->findAll();

        $pager = $result['page'];
        $result = $result['result'];
        include $this->display( $c );
    }

    private function index_addOrder( $c, $a, $type ) {
        include $this->display( $c );
    }
    public function addOrder( $c, $a, $type ) {
        include $this->display( $c );
    }
    public function postGoods( $c, $a, $type ) {
        $result['orderNo'] = G( 'orderNo') ;
        include $this->display( $c );
    }
    public function evaluateGoods( $c, $a, $type ) {
        $result['orderNo'] = G( 'orderNo') ;
        include $this->display( $c );
    }
    public function backGoods( $c, $a, $type ) {
        $result['orderNo'] = G( 'orderNo') ;
        include $this->display( $c );
    }
    public function cancelGoods( $c, $a, $type ) {
        $result['orderNo'] = G( 'orderNo') ;
        include $this->display( $c );
    }
    public function editOrder( $c, $a, $args) {

        $this->table = 'order';
        $condition = array('orderNo'=>  $args['id']);
        $result = $this->find($condition);

        include $this->display( $c );
    }


    public function edit( $c, $a ) {
        if ( !$id = intval( G( 'id' ) ) ) {
            $this->error();
            exit;
        }
        $this->table = ''; // 数据库表名
        $result = $this->find( array( 'id' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    public function add( $c, $a ) {
        include $this->display( $c );
    }

    public function saveEvaluate() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /**zouhong-edit**/
        $rows['evaluateDate'] = date("YmdHis");
        /**zouhong-edit-end**/

        /***评价处理***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'evaluate'; // 数据库表名
        $condition = array('orderNo'=> $_POST['orderNo']);
        $result = $this->find($condition, 'orderNo');
        if (  $result ) {
            $msg = $this->update( array( 'id' => $result['id'] ), $rows ) ? 'success' : 'failed'; //更新订单
            $id = $result['id'];
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加订单
            $res = $this->find(null, null, "max(id)");
            $id = intval($res['max(id)']);
        }

        /***商品订单流转表处理***/
        unset($rows);
        $rows['evalueateId'] = $id;
        $rows['orderStatus'] = 3;    /*'3' means 已评价*/
        $condition = array('orderNo'=>$_POST['orderNo']);
        $this->table = 'orderRoam';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        $this->table = 'order';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        exit( $msg );
    }

    public function savePost() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /**zouhong-edit**/
        $rows['postDate'] = date("YmdHis");
        /**zouhong-edit-end**/

        /***评价处理***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'postGoods'; // 数据库表名
        $condition = array('goodsNo'=> $_POST['goodsNo']);
        $result = $this->find($condition, 'goodsNo');
        if ( $result ) {
            $msg = $this->update( array( 'id' => $result['id'] ), $rows ) ? 'success' : 'failed'; //更新订单
            $id = $result['id'];
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加订单

            $res = $this->find(null, null, "max(id)");
            $id = intval($res['max(id)']);
        }

        /***商品订单流转表处理***/

        unset($rows);
        $rows['postId'] = $id;
        $rows['orderStatus'] = 1;    /*'1' means 已发货*/
        $condition = array('orderNo'=>$_POST['goodsNo']);
        $this->table = 'orderRoam';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        $this->table = 'order';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        exit( $msg );
    }

    public function saveBack() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /**zouhong-edit**/
        $rows['cancelDate'] = date("YmdHis");
        /**zouhong-edit-end**/

        /***换货记录***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'exchangeOrder'; // 数据库表名
        $condition = array('orderNo'=> $_POST['orderNo']);
        $result = $this->find($condition, 'exchangeOrderNo');
        if (  $result ) {
            $msg = $this->update( array( 'exchangeOrderNo' => $result['exchangeOrderNo'] ), $rows ) ? 'success' : 'failed'; //更新订单
            $id = $result['exchangeOrderNo'];
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加订单
            $res = $this->find(null, null, "max(exchangeOrderNo)");
            $id = intval($res['max(exchangeOrderNo)']);
        }

        /***商品订单流转表处理***/
        unset($rows);
        $rows['exchangeId'] = $id;
        $rows['orderStatus'] = 2;    /*'2' means 换货终止订单*/
        $condition = array('orderNo'=>$_POST['orderNo']);
        $this->table = 'orderRoam';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新顾客订单表

        $this->table = 'order';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        exit( $msg );
    }
    public function saveCancel() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        /**zouhong-edit**/
        $rows['cancelDate'] =date("YmdHis");
        /**zouhong-edit-end**/

        /***评价处理***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'cancelOrder'; // 数据库表名
        $condition = array('orderNo'=> $_POST['orderNo']);
        $result = $this->find($condition, 'cancelOrderNo');
        if (  $result ) {
            $msg = $this->update( array( 'cancelOrderNo' => $result['cancelOrderNo'] ), $rows ) ? 'success' : 'failed'; //更新订单
            $id = $result['cancelOrderNo'];
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加订单
            $res = $this->find(null, null, "max(cancelOrderNo)");
            $id = intval($res['max(cancelOrderNo)']);
        }

        /***商品订单流转表处理***/

        unset($rows);
        $rows['cancelOrderId'] = $id;
        $rows['orderStatus'] = 4;    /*'4' means 退货*/
        $condition = array('orderNo'=>$_POST['orderNo']);
        $this->table = 'orderRoam';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        $this->table = 'order';
        $msg = $this->update($condition, $rows ) ? 'success' : 'failed'; //更新商品流转表

        exit( $msg );
    }


    public function save() {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }
        /**zouhong-edit**/
        $rows['orderDate'] = date("YmdHis");
        $rows['orderStatus'] = 0;
        /**zouhong-edit-end**/

       /***订单表处理***/
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'order'; // 数据库表名
        if ( $id = intval( G( "id" ) ) ) {
            $msg = $this->update( array( 'id' => $id ), $rows ) ? 'success' : 'failed'; //更新订单
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加订单
        }

        $res = $this->find(null, null, "max(orderNo)");
        $orderNo = intval($res['max(orderNo)']);

        /***商品订单流转表处理***/
        unset($rows);
        $rows['orderNo'] = $_POST['orderNo'];
        $rows['taobaoOrderNo'] = $_POST['taobaoOrderNo'];
        $rows['orderStatus'] = 1;   //'1':已创建
        $rows['orderCreateId'] = $_SESSION['userId'];
        $this->table = 'orderRoam';

        $msg = $this->create($rows)?'success':'failed';

        /***订单表处理***/
       $goodsNo = $_POST['goodsNo'];
       $condition = array('id'=>$goodsNo);
       $this->table = 'bsInfo';
       $result = $this->find($condition, 'id');
       $supplierId = $result['supplierId'];

        if($supplierId == null)
            return;

        $this->table = 'user';
        $sql = array('supplierId'=>$supplierId, 'rights'=>1 );
        $arrayId = $this->find($sql, 'id', 'id');

        /**员工订单表处理**/
        $this->table = 'employeeOrder';

        foreach ( $arrayId as $key => $val ) {
             $rows['customId'] = $val;
             $rows['orderId'] = $orderNo;
             $rows['STATUS'] = 0;
             $msg = $this->create($rows)? 'success':'failed';
        }

        exit( $msg );
    }

    public function search_order( $c, $a ) {
        $this->table = 'order'; // 数据库表名
        $type = G( 'type', 'orderMan' );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "orderNo ={$key} " ) : '';
        }

        $this->page_size = 2;
        $this->pk = 'orderNo';
        $result = $this->Paged( G( 'page' ), $this->page_size )->findAll($where);

        $pager = $result['page'];
        $result = $result['result'];

        include $this->display( $c ); // 加载模板

    }

    public function search_myorder( $c, $a ) {
        $this->table = 'order'; // 数据库表名
        $type = G( 'type', 'myOrder' );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "orderNo ={$key} " ) : '';
        }

        $this->page_size = 2;
        $this->pk = 'orderNo';
        $result = $this->Paged( G( 'page' ), $this->page_size )->findAll($where);

        $pager = $result['page'];
        $result = $result['result'];

        include $this->display( $c ); // 加载模板

    }
}